
var netUtil = require('/../../utils/netUtil.js');
var util = require('/../../utils/util.js');
var app = getApp();

Page({
  data:{
		voucherId : null,
		saveOrderDetail : null,
		comboArray : getApp().globalData.comboArray ,
		typeAndGoodArray : [[] , []],       //用户显示 选择品种picker的属性
		respTypeAndGoodArray : null
  },
  onLoad:function(options){
  	//村查单订单号
  	var voucherId = options.voucherId;
  	
  	if(voucherId != undefined && voucherId != null) {
  		this.setData({
  			voucherId : voucherId
  		});
  	}
  	
  	app.getUserOpedId(this.getAllTypeAndGoods);
  	
  	app.getUserOpedId(this.querySaveOrderDetailArray);
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  //提交存茶申请单修改
  submitSaveTeaUpdate : function(e) {
  	var that = this;
  	
  	var reqData = {};
  	reqData.logisticsCompany = that.data.saveOrderDetail.logisticsCompany;
  	reqData.logisticsCode = that.data.saveOrderDetail.logisticsCode;
  	reqData.voucherId = that.data.voucherId;
  	
  	var inItems = [];
  	
  	for(var i=0;i<that.data.saveOrderDetail.items.length;i++) {
  		var item = that.data.saveOrderDetail.items[i];
  		
  		if(item.goodsId != undefined && item.goodsId != null && item.goodsId != ''
  		   && item.packageQuantity != undefined && item.packageQuantity != null && item.packageQuantity != ''
  		   && item.combo != undefined && item.combo != null && item.combo != '') {
  		   	  
  		   	  inItems.push({
  		   	  	goodsId : item.goodsId,
  		   	  	packageQuantity : item.packageQuantity,
  		   	  	unitQuantity : item.unitQuantity,
  		   	  	combo : item.combo
  		   	  });
  		   	  
  		   }
  	}
  	
  	if(inItems.length <= 0) {
  		netUtil.showAlertDialog("提示", '没有有效的记录', false, "确定", null, null);
			return;
  	}
  	
  	reqData.inItems = inItems;
  	
  	var params = {
  			method : 'wechat.wisen.in.voucher.update',
  			openId : app.globalData.userInfo.openid,
  			data : reqData
  	};
  	
  		netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					
					netUtil.hideLoadingDialog(that);
					
					if(resp.ret == app.globalData.respSuccessCode) {
								
							//存茶成功后，跳转到订单页	
							wx.reLaunch({
								url: '/pages/order/order?from=saveTea'
							});
						
 					} else {
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
					}
					
				},
				
				onError: function(res) {
					 console.log("fail");
					 netUtil.hideLoadingDialog(that);
					 netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
				
			}).send();
  	
  },
  
  	//查询所有类别和商品
	getAllTypeAndGoods : function() {
		var that = this;
		
		var params = {
  		method : 'wechat.wisen.goods.all.query',
  		openId : app.globalData.userInfo.openid
  	}
		
		netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					console.log(resp);
					
					netUtil.hideLoadingDialog(that);
					
					if(resp.ret == app.globalData.respSuccessCode) {
										
						var data = resp.data;
						
						var typeArray = data[0];
						var goodsArray = data[1];
						
						that.data.typeAndGoodArray[0] = typeArray;
						that.data.typeAndGoodArray[1] = goodsArray[0];
						
						that.setData({
							typeAndGoodArray : that.data.typeAndGoodArray,
							respTypeAndGoodArray : data
						});
						
 					} else {
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
					}
					
				},
				
				onError: function(res) {
					console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
				
			}).send();
		
	},
  
  querySaveOrderDetailArray : function() {
  	var that = this;
  	var params = {
  		method : 'wechat.wisen.in.inItems.query',
  		openId : app.globalData.userInfo.openid,
  		data : {
  			voucherId : that.data.voucherId
  		}
  	};
  	
  	netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  		onPre: function() {
				netUtil.showLoadingDialog(that);
			},
			onSuccess : function(resp) {
			 
				//如果请求成功
				if(resp.ret == app.globalData.respSuccessCode) {
					
					var saveOrderDetail = resp.data;
		
					that.setData({
						saveOrderDetail : saveOrderDetail
					});
				
				} else {
						
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
				}				
			},
			onError: function(msgCanShow, code, hiddenMsg) {
        console.log("fail");
				netUtil.hideLoadingDialog(that);
				netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
			}
  		
  	}).send();
  	
  },
  
  addSaveTeaItem : function(e) {
		var teaItem = {
			goodsId : '',
			goodsName : '',
			unitQuantity : 0.0,
			packageQuantity : '',
			totalQuantity :'',
			combo : this.data.comboArray[0].comboKey
			
		};
		this.data.saveOrderDetail.items.push(teaItem);
		this.setData({
			saveOrderDetail : this.data.saveOrderDetail
		});
	},
	
	//选择套餐
	 chooseCombo: function(e) {
   	
   	var value = e.detail.value;  //选中的数组的下表
   	var initemsindex = e.currentTarget.dataset.initemsindex;
   	
   	this.data.saveOrderDetail.items[initemsindex].combo = this.data.comboArray[value].comboKey;
   	
   	this.setData({
   		saveOrderDetail : this.data.saveOrderDetail
   	});
   	
  },
	
	reduceSaveTeaItem : function(e) {
		var initemsindex = e.currentTarget.dataset.initemsindex;
		this.data.saveOrderDetail.items.splice(initemsindex,1);
		this.setData({
			saveOrderDetail : this.data.saveOrderDetail
		});
	},
	
	//列值改变事件
  bindTypeAndGoodsColumnChange : function(e) {
  	var column = e.detail.column;
  	var value = e.detail.value;
  
  	//如果改变第一列
  	if(column == 0) {
  		this.data.typeAndGoodArray[1] = this.data.respTypeAndGoodArray[1][value];
  		this.setData({
  			typeAndGoodArray : this.data.typeAndGoodArray
  		});
  	}
  },
  
  //列值确定事件
  bindTypeAndGoodsChange :function(e) {
  	
  	var value = e.detail.value;
  	
  		//由于小程序的bug，第二列动态切换后，如果选择切换后默认的第一个，数据的第二个元素返回 null
  	for(var i=0;i<value.length;i++) {
  		var columnValue = value[i];
  		if(columnValue == null || columnValue == undefined) {
  			value[i] = 0;
  		}
  	}
  	
  	var arrayIndex = e.currentTarget.dataset.initemsindex;
  	var selectGoddIndex = value[1];
  	
  	//1.设置数据对应下标的值
  	var selectedGoodObj = this.data.typeAndGoodArray[1][selectGoddIndex];
  	this.data.saveOrderDetail.items[arrayIndex].goodsId = selectedGoodObj.goodsId;
  	this.data.saveOrderDetail.items[arrayIndex].goodsName = selectedGoodObj.goodsName;
  	
  	this.setData({
  		saveOrderDetail : this.data.saveOrderDetail
  	});
  	
  	//2.所有选项回复到最初始的状态
  	//this.bindTypeAndGoodsCancel();
  },
  
  //点击取消选择，所有选项回复最初始的状态
  bindTypeAndGoodsCancel : function(e) {
  	this.data.typeAndGoodArray[0] = this.data.respTypeAndGoodArray[0];
  	this.data.typeAndGoodArray[1] = this.data.respTypeAndGoodArray[1][0];
  	
  	this.setData({
  		typeAndGoodArray : this.data.typeAndGoodArray
  	});
  },
  
	//输入物流公司
	inputLogisticsCompany : function(e) {
		var logisticsCompany = e.detail.value;
		
		this.data.saveOrderDetail.logisticsCompany = logisticsCompany
		
		this.setData({
			saveOrderDetail : this.data.saveOrderDetail
		});
	},
	
	inputLogisticsCode : function(e) {
			var logisticsCode = e.detail.value;
			this.data.saveOrderDetail.logisticsCode = logisticsCode
			this.setData({
				saveOrderDetail: this.data.saveOrderDetail
			});
	},
	
	//选择数量
	inputTeaCount : function(e) {
		var initemsindex = e.currentTarget.dataset.initemsindex;
		var packageQuantity = e.detail.value;
		
		this.data.saveOrderDetail.items[initemsindex].packageQuantity = packageQuantity;
		
		this.setData({
			saveOrderDetail : this.data.saveOrderDetail
		});	
	}
})